#!/usr/bin/python
# -*- coding: UTF-8 -*-

"""
@author: jiu
@contact: 643562684@qq.com
@time: 2022/4/1 17:27
"""
from time import sleep

from browser.chrome import CHROME
from common.read_Yaml import Read_Yaml
from Conf.setting import YAML_FILE_LIST
from PageProjects.common_page import CommonPage


class ApplyPage(CommonPage):
    """
        提交申请贷款
    """

    # 数据页加载
    CommonPage.element_data.update(
        Read_Yaml(YAML_FILE_LIST["al"]).data
    )

    def remove_readonly(self, loc):
        """删除元素的readonly属性"""
        _el = self.get_element(loc)
        js = r"arguments[0].removeAttribute(\"readonly\")"
        self.execute_js(js, _el)

    def apply_to_loan(self, name=None, username=None, password=None):
        self.open_url(self._url)
        self.move_to_element(self.i_want_a_loan, page_action="鼠标移动到我要贷款上")
        self.el_click(self.i_want_a_loan_apply, page_action="点击申请贷款")
        self.element_input(self.login_username, value=username or self.user_info["username"],
                           page_action="登入界面，定位用户名输入框输入")
        self.element_input(self.login_pwssword, value=password or self.user_info["password"],
                           page_action="登入界面，定位密码输入框输入")
        self.element_input(self.login_Jverify, value=self.user_info["verify_code"], page_action="登入界面，定位验证码输入框输入")
        self.el_click(self.i_want_a_loan_login, page_action="点击登入按钮")
        if name:
            self.loan_type_btn[1] = self.loan_type_btn[1].replace("旅游基金", name)
            self.el_click(self.loan_type_btn, page_action=f"点击 {name}")
        else:
            # 业务贷款项 以旅游贷款为例子
            self.el_click(self.loan_type_btn, page_action="点击旅游贷款")
        _title = self._dt.random_text[:10]
        #  添加审核材料
        self.audit_materials.append(_title)

        self.element_input(self.loan_title, value=_title, page_action="定位投资标题,输入")
        self.el_click(self.loan_effect, page_action="定位借款用途，进行点击")
        self.el_click(self.loan_effect_turnover, page_action="借款用途下选择，短期周转")
        # 抵押默认没有
        self.element_input(self.loan_amount_of_money, value=self._dt.random_money, page_action="定位金额，金额为50的倍数")
        # 还款周期 没得选
        self.el_click(self.loan_repayment_term, page_action="定位借款期限，进行点击")
        self.el_click(self.loan_repayment_term_24, page_action="选择还款期限为24个月")
        _min_lv = self.get_text(self.loan_year_range_min, page_action='获取最小的年率率')
        _max_lv = self.get_text(self.loan_year_range_max, page_action='获取最大的年率率')
        self.element_input(self.loan_year_interest_rate, value=self._dt.random_lvli(_min_lv, _max_lv),
                           page_action="定位年率率输入框进行输入")
        self.el_click(self.loan_repayment_method, page_action="定位还款方式,进行点击")
        self.el_click(self.loan_repayment_method_expire, page_action="选择到期还本息,进行点击")
        # 担保机构默认
        self.el_click(self.loan_term, page_action="定位筹标期限,进行点击")
        self.el_click(self.loan_term_day_15, page_action="选择筹表日期为15天")

        # ***********************
        self.switch_frame(self.loan_message, page_action="切换到markdown的插件内")
        self.element_input(self.loan_message_body, value=self._dt.random_text, page_action="定位借款描述,进行文本输入")
        self.exit_frame_window(page_action="退出 富文本编辑框的frame")

        # ***********************

        #  这一段是对图片的操作 浏览器问题按钮点击没反应

        # self.el_click(self.loan_show_image_box, page_action="点击展示资料的checkbox按钮")
        # self.element_input(self.loan_show_info, value=self._dt.random_text[:4], page_action="定位展示资料中的名称,进行随机输入")
        # self.el_click(self.loan_select_image_btn, page_action="点击选择图片的按钮")
        # self.el_click(self.loan_upload_image, page_action="点击切换到本地上传")

        # self.remove_readonly(self.load_upload_image_path)
        # _el = self.get_element(self.load_upload_image_path)
        # self.driver.execute_script("arguments[0].removeAttribute(\"readonly\")", _el)

        # C:\fakepath\八仙花.png
        # default_image_path = r'C:\fakepath\m.jpg'
        # self.element_input(self.load_upload_image_path, value=default_image_path, page_action="上传图片地址")
        # self.el_click(self.load_accept_upload, page_action="点击确定按钮")

        #  这一段是对图片的操作 浏览器问题按钮点击没反应

        # self.el_click(self.loan_accept_agreement, page_action="点击同意协议")   #  默认已经是选址的状态

        self.el_click(self.submit_for_review, page_action="点击提交审核")
        self.alert_accept(page_action="确定提交审核")
